package com.xingda.packing.minitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingda.packing.common.dto.PageDto;
import com.xingda.packing.minitor.dto.PkWheelDto;
import com.xingda.packing.minitor.entity.PkWheel;
import com.xingda.packing.minitor.mapper.PkWheelMapper;
import com.xingda.packing.minitor.service.IPkWheelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author anxiaoning
 * @since 2021-07-03
 */
@Service
public class PkWheelServiceImpl extends ServiceImpl<PkWheelMapper, PkWheel> implements IPkWheelService {

    @Resource
    private PkWheelMapper pkWheelMapper;

    @Override
    public List<Map<String, Object>> getPkWheelByValue(PageDto dto) {
        List<Map<String, Object>> list = this.pkWheelMapper.getPkWheelByValue(dto);
        if (list.size() == 0) {
            return null;
        }
        return list;
    }

    @Override
    public Page<PkWheelDto> getPkWheelPage(PageDto dto) {

        LambdaQueryWrapper<PkWheel> pkWheelLambdaQueryWrapper = Wrappers.lambdaQuery();
        pkWheelLambdaQueryWrapper.ge(PkWheel::getScanTime, dto.getStart());
        pkWheelLambdaQueryWrapper.lt(PkWheel::getScanTime, dto.getEnd());
        pkWheelLambdaQueryWrapper.eq(PkWheel::getIsFinished, 1);

        Page<PkWheel> pkWheelPage = new Page<>(dto.getCurrent(), dto.getPagesize());
        this.baseMapper.selectPage(pkWheelPage, pkWheelLambdaQueryWrapper);

        Page<PkWheelDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pkWheelPage, dtoPage);

        return dtoPage;
    }

    @Override
    public List<Map<String, Object>> getPkWheelByTime(PageDto dto) {

        return this.pkWheelMapper.getPkWheelByTime(dto);
    }

    @Override
    public boolean savePkWheel(PkWheelDto dto) {

        PkWheel pkWheel = new PkWheel();
        BeanUtils.copyProperties(dto, pkWheel);

        return this.save(pkWheel);
    }

    /**
     * 根据工字轮 和 装箱详情表 查询已装箱的工字轮信息
     */
    @Override
    public List<PkWheel> getPkWheelByPutTime(List<Integer> taskIds) {

        return this.pkWheelMapper.getPkWheelByPutTime(taskIds);
    }
}
